package com.bestcem.xm.member.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.bestcem.xm.common.core.uitls.StringUtil;
import com.bestcem.xm.member.dao.BaseDao;
import com.bestcem.xm.member.dao.SyncRuleDao;
import com.bestcem.xm.member.dao.mapper.SyncRuleMapper;
import com.bestcem.xm.member.entity.mongo.SyncRule;
import com.bestcem.xm.member.entity.mysql.SyncRuleEntity;
import com.bestcem.xm.member.entity.pojo.SyncRuleDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 同步规则数据访问
 * @date 2021-08-02
 */
@Repository("syncRuleDao")
public class SyncRuleDaoImpl extends BaseDao implements SyncRuleDao {

    @Autowired
    private SyncRuleMapper syncRuleMapper;

    /**
     * 获取同步规则
     *
     * @param orgId
     * @param memberType
     * @return
     */
    @Deprecated
    @Override
    public SyncRule get(String orgId, Integer memberType) {
        return null;
    }

    /**
     * 更新同步规则
     *
     * @param syncRule
     */
    @Deprecated
    @Override
    public SyncRule update(SyncRule syncRule) {
        return null;
    }

    @Deprecated
    @Override
    public SyncRule findById(String id) {
        return null;
    }

    /**
     * 获取同步规则
     *
     * @param orgId 组织标识
     * @param memberType 联系人类型 0-个人联系人 1-机构联系人 2-内部联系人 3-隐藏的 4-项目
     * @return SyncRuleDo 同步规则信息
     */
    @Override
    public SyncRuleDO getByOrderIdAndMemberType(@NotNull String orgId, @NotNull Integer memberType) {
        SyncRuleEntity parameter = new SyncRuleEntity();
        parameter.setOrgId(orgId);
        parameter.setMemberType(memberType);
        List<SyncRuleEntity> syncRuleEntities = syncRuleMapper.selectBySyncRule(parameter);
        if (CollectionUtils.isEmpty(syncRuleEntities)) {
            return null;
        }
        return SyncRuleDO.toDo(syncRuleEntities.get(0));
    }

    /**
     * 更新同步规则
     *
     * @param syncRuleDo 同步规则信息
     * @return SyncRuleDo 同步规则信息
     */
    @Override
    public SyncRuleDO update(@NotNull SyncRuleDO syncRuleDo) {
        syncRuleMapper.updateBySyncruleId(SyncRuleDO.toEntity(syncRuleDo));
        return findBySyncRuleId(syncRuleDo.getSyncruleId());
    }

    /**
     * 创建同步规则
     *
     * @param syncRuleDo 同步规则信息
     */
    @Override
    public void create(SyncRuleDO syncRuleDo) {
        syncRuleDo.setSyncruleId(StringUtil.getId());
        syncRuleMapper.insert(SyncRuleDO.toEntity(syncRuleDo));
    }

    /**
     * 根据id获取同步规则信息
     *
     * @param syncRuleId 同步规则id
     * @return SyncRuleDo 同步规则信息
     */
    @Override
    public SyncRuleDO findBySyncRuleId(@NotNull String syncRuleId) {
        SyncRuleEntity parameter = new SyncRuleEntity();
        parameter.setSyncruleId(syncRuleId);
        List<SyncRuleEntity> result = syncRuleMapper.selectBySyncRule(parameter);
        if (CollectionUtils.isEmpty(result)) {
            return null;
        }
        return SyncRuleDO.toDo(result.get(0));
    }


    /**
     * 批量更新同步规则信息
     *
     * @param syncRuleDOS 待更新同步规则信息列表
     * @return 更新的结果
     */
    @Override
    public Boolean batchUpdateSyncRule(List<SyncRuleDO> syncRuleDOS) {
        for (int i = 0; i < syncRuleDOS.size(); i++) {
            syncRuleMapper.updateTypeByOrgIdAndMemberType(SyncRuleDO.toEntity(syncRuleDOS.get(i)));
        }
        return true;
    }

    @Override
    public List<String> findByOrgIdsAndMemberTypeAndWechatSyncEnabled(List<String> orgIds, int memberType, int syncType) {
        List<SyncRuleEntity> result = syncRuleMapper.findByOrgIdsAndMemberTypeAndWechatSyncEnabled(orgIds, memberType, syncType);
        if (CollUtil.isNotEmpty(result)) {
            return result.stream().map(SyncRuleEntity::getOrgId).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
